List.initUserSearchField   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 47
rs 7.712
cc 7
1
import 'devbridge-autocomplete'
2
3
export class List {
4
    constructor() {
5
        this.initUserSearchField()
6
        this.initDomainRolesListener()
7
    }
8
9
    initUserSearchField() {
10
        let modal = $('#importUserModal')
11
        let el = $('#user_name', modal)
12
13
        el.devbridgeAutocomplete({
14
            serviceUrl: el.attr('data-url'),
15
            type: 'get',
16
            paramName: 'q',
17
            onSearchStart: () => {
18
                el.removeClass('invalid')
19
                $('#user_id', modal).val('')
20
                $('#domain_roles', modal).addClass('hide')
21
                $('button.save', modal).attr('disabled', true)
22
            },
23
            onSearchComplete: (query, suggestions) => {
24
                if (suggestions.length === 0) {
25
                    el.addClass('invalid')
26
                }
27
            },
28
            onSelect: (record) => {
29
                let hasRoles = $('#domain_roles select').formSelect('getSelectedValues').length > 0
30
31
                $('#user_id', modal).val(record.data.id)
32
                $('#domain_roles', modal).removeClass('hide')
33
                $('button.save', modal).attr('disabled', !hasRoles)
34
            },
35
            showNoSuggestionNotice: false,
36
            transformResult: function(response, originalQuery) {
0 ignored issues
show
Unused Code introduced by
The parameter originalQuery is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
                let results = {
38
                    suggestions: []
39
                }
40
41
                let originalResults = JSON.parse(response)
42
                for (let result of originalResults) {
43
                    results.suggestions.push({
44
                        value: result.title,
45
                        data: {
46
                            module: result.type,
47
                            id: result.searchable.id
48
                        }
49
                    })
50
                }
51
52
                return results
53
            }
54
        })
55
    }
56
57
    initDomainRolesListener() {
58
        let modal = $('#importUserModal')
59
60
        $('#domain_roles select', modal).on('change', event => {
61
            let hasRoles = $(event.currentTarget).formSelect('getSelectedValues').length > 0
62
            $('button.save', modal).attr('disabled', !hasRoles)
63
        })
64
    }
65
}